package learning.spring.helloworld.factory;

import learning.spring.helloworld.providers.MessageProvider;
import learning.spring.helloworld.renderers.MessageRenderer;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: bibro
 * Date: 18.02.14
 * Time: 20:10
 * To change this template use File | Settings | File Templates.
 */
public class MessageSupportFactory {

    private static MessageSupportFactory instance = null;
    private Properties properties;
    private MessageRenderer messageRenderer;
    private MessageProvider messageProvider;

    private MessageSupportFactory() {
        properties = new Properties();

        try {
            properties.load(MessageSupportFactory.class.getClassLoader().getResourceAsStream("msf.properties"));
            String rendererClass = properties.getProperty("renderer.class");
            String providerClass = properties.getProperty("provider.class");

            messageRenderer =  (MessageRenderer)Class.forName(rendererClass).newInstance();
            messageProvider =  (MessageProvider)Class.forName(providerClass).newInstance();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InstantiationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }


    public static MessageSupportFactory getInstance() {
        return instance;
    }

    static {
        instance = new MessageSupportFactory();
    }

    public MessageRenderer getMessageRenderer() {
        return messageRenderer;
    }

    public void setMessageRenderer(MessageRenderer messageRenderer) {
        this.messageRenderer = messageRenderer;
    }

    public MessageProvider getMessageProvider() {
        return messageProvider;
    }

    public void setMessageProvider(MessageProvider messageProvider) {
        this.messageProvider = messageProvider;
    }
}
